import time

import pytest
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
from dama import Dama

@pytest.fixture(scope='session')
def getcookie():
    d=getcode()
    headers={
        'content-type':'application/x-www-form-urlencoded; charset=UTF-8',
        'cookie':f'JSESSIONID={d.get("JSESSIONID")};JEECGINDEXSTYLE={d.get("JEECGINDEXSTYLE")};ZINDEXNUMBER={d.get("ZINDEXNUMBER")}'
    }
    return headers

def getcode():
    driver=webdriver.Chrome()
    driver.get('http://10.59.9.32/jeewms/loginController.do?login')
    driver.save_screenshot(r'D:\gaoliu-2211A\work4\img.png')
    ele=driver.find_element(By.XPATH,'//*[@id="randCodeImage"]')
    size=ele.size
    loca=ele.location
    x=509
    y=252
    print(size,loca,x,y)
    framd=(x,y,x+size['width'],y+size['height'])
    im=Image.open(r'D:\gaoliu-2211A\work4\img.png')
    crop=im.crop(framd)
    crop.save(r'D:\gaoliu-2211A\work4\img2.png')
    dm=Dama('weimingxue','123456','970647')
    with open('img2.png','rb')as f:
        yazm=f.read()
    jieguo=dm.PostPic(yazm,1902)
    result=jieguo['pic_str']
    print(result)


    time.sleep(3)
    driver.find_element(By.XPATH,'//*[@id="userName"]').clear()
    driver.find_element(By.XPATH,'//*[@id="userName"]').send_keys('admin')
    time.sleep(3)
    driver.find_element(By.XPATH,'//*[@id="password"]').clear()
    driver.find_element(By.XPATH,'//*[@id="password"]').send_keys('llg123')

    # img=driver.find_element(By.XPATH,'//*[@id="randCodeImage"]')

    time.sleep(3)
    driver.find_element(By.XPATH,'//*[@id="randCode"]').send_keys(result)
    time.sleep(3)
    driver.find_element(By.XPATH,'//*[@id="but_login"]').click()
    time.sleep(3)
    dic={}
    for d in driver.get_cookies():
        for key,value in d.items():
            if value == 'JSESSIONID':
                dic['JSESSIONID']=d.get('value')
            if value == 'JEECGINDEXSTYLE':
                dic['JEECGINDEXSTYLE']=d.get('value')
            if value == 'ZINDEXNUMBER':
                dic['ZINDEXNUMBER']=d.get('value')
    driver.quit()
    return dic



